package de.fh.ka.as.car;


import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class SensorTestOld extends Activity {
    private GyroVisualizer mGyroView;
    private SensorManager mSensorManager;
    private Sensor mGyroSensor, mAccSensor, mMagSensor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGyroView = new GyroVisualizer(this);

        setContentView(mGyroView);

        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        mGyroSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        mAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mMagSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    private SensorEventListener mGyroListener = new SensorEventListener() {

        private static final float MIN_TIME_STEP = (1f / 40f);
        private long mLastTime = System.currentTimeMillis();
        private float mRotationX, mRotationY, mRotationZ;

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            float x = values[0];
            float y = values[1];
            float z = values[2];

            float angularVelocity = z * 0.96f; // Minor adjustment to avoid drift on Nexus S

            // Calculate time diff
            long now = System.currentTimeMillis();
            float timeDiff = (now - mLastTime) / 1000f;
            mLastTime = now;
            if (timeDiff > 1) {
                // Make sure we don't go bananas after pause/resume
                timeDiff = MIN_TIME_STEP;
            }

            mRotationX += x * timeDiff;
            if (mRotationX > 0.5f)
                mRotationX = 0.5f;
            else if (mRotationX < -0.5f)
                mRotationX = -0.5f;

            mRotationY += y * timeDiff;
            if (mRotationY > 0.5f)
                mRotationY = 0.5f;
            else if (mRotationY < -0.5f)
                mRotationY = -0.5f;

            mRotationZ += angularVelocity * timeDiff;

            mGyroView.setGyroRotation(mRotationX, mRotationY, mRotationZ);
        }
    };

    private SensorEventListener mAccListener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            float x = values[0];
            float y = values[1];
            // float z = values[2];

            // Ignoring orientation since the activity is using screenOrientation "nosensor"

            mGyroView.setAcceleration(-x, y);
        }
    };

    private SensorEventListener mMagListener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            float x = values[0];
            float y = values[1];
            // float z = values[2];

            mGyroView.setMagneticField(x, -y);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(mGyroListener, mGyroSensor, SensorManager.SENSOR_DELAY_UI);
        mSensorManager.registerListener(mAccListener, mAccSensor, SensorManager.SENSOR_DELAY_UI);
        mSensorManager.registerListener(mMagListener, mMagSensor, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(mGyroListener, mGyroSensor);
        mSensorManager.unregisterListener(mAccListener, mGyroSensor);
        mSensorManager.unregisterListener(mMagListener, mMagSensor);
    }	

//
//	/** Called when the activity is first created. */
//
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
//
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.main);
//		view = findViewById(R.id.textView);
//		view.setBackgroundColor(Color.GREEN);
//
//		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
//		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
//				SensorManager.SENSOR_DELAY_NORMAL);
//		lastUpdate = System.currentTimeMillis();
//	}
//
//	@Override
//	public void onDestroy() {
//		super.onDestroy();
//		sensorManager.unregisterListener(this);
//	}
//
//	@Override
//	public void onSensorChanged(SensorEvent event) {

//		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
////			
////			
//			float[] values = event.values;
//			// Movement
//			float x = values[0];
//			float y = values[1];
//			float z = values[2];
////
//			float accelationSquareRoot = (x * x + y * y + z * z);
//		
////					/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
////			long actualTime = System.currentTimeMillis();
//
//////				if (actualTime - lastUpdate < 200) {
//////					return;
//////				}
//////				lastUpdate = actualTime;
//				String output="x="+x+" ,y="+y+" ,z="+z;
//				String output2=""+accelationSquareRoot;
//				String output3="timestamp="+event.timestamp;
//				Log.i("Sensor vectors", output);
//				Log.i("Sensor accelationSquareRoot", output2);
//				Log.i("Sensor timestamp", output3);
//				view.setBackgroundColor(Color.BLUE);
//////				Toast.makeText(this, output, Toast.LENGTH_SHORT).show();
//////				Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT).show();
//////				if (color) {
//////					view.setBackgroundColor(Color.GREEN);
//////
//////				} else {
//////					view.setBackgroundColor(Color.RED);
//////				}
//////				color = !color;
//		}
////		if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
//////			event.
////		}
//
//	}
//
//	@Override
//	public void onAccuracyChanged(Sensor sensor, int accuracy) {
//		// TODO Auto-generated method stub
//
//	}
//
//	@Override
//	protected void onResume() {
//		super.onResume();
//		// register this class as a listener for the orientation and
//		// accelerometer sensors
//		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
//				SensorManager.SENSOR_DELAY_NORMAL);
//	}
//
//	@Override
//	protected void onPause() {
//		// unregister listener
//		sensorManager.unregisterListener(this);
//		super.onStop();
//	}
//
//	@Override
//	public void onClick(DialogInterface dialog, int which) {
//		// TODO Auto-generated method stub
//		
//	}

}
